package com.example.dragablelistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import sample4.dnd.DragListAdapter;
import sample4.provider.MyDatabase.MyTable;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class MyCursorAdapter extends DragListAdapter {
	private static final String TAG = "MyCursorAdapter";
	private int indexId = 0;
	private int indexName = 0;
	private int indexPlayOrder = 0;
	private HashMap<Integer, Item> mSortedItems;

	public MyCursorAdapter(Context context, int layout, Cursor c,
			boolean autoRequery) {
		super(context, layout, c, autoRequery);
		mSortedItems = new HashMap<Integer, MyCursorAdapter.Item>();
		if (c != null) {
			initCursorIndex(c);
		}
	}

	@Override
	public Cursor swapCursor(Cursor newCursor) {
		if (newCursor != null) {
			initCursorIndex(newCursor);
		}
		return super.swapCursor(newCursor);
	}

	private void initCursorIndex(Cursor c) {
		mSortedItems.clear();
		indexName = c.getColumnIndex(MyTable.Columns.NAME);
	}

	/**
	 * アイテムの順番を切り替える
	 * 
	 * @param selectPositon
	 *            選択されたアイテムの順番
	 * @param targetPosition
	 *            　選択されたアイテムが移動する順番
	 */
	private void swapItem(int selectPositon, int targetPosition) {

		final Item select = getItem(selectPositon);
		final Item target = getItem(targetPosition);

		// swap order
		int swapOrder = target.mNewPlayOrder;
		target.mNewPlayOrder = select.mNewPlayOrder;
		select.mNewPlayOrder = swapOrder;

		// swap items
		mSortedItems.put(selectPositon, target);
		mSortedItems.put(targetPosition, select);

		// clear items
		if (!select.isChanged()) {
			mSortedItems.remove(targetPosition);
		}
		if (!target.isChanged()) {
			mSortedItems.remove(selectPositon);
		}
	}

	/**
	 * 移動されたアイテムのリストを取得する
	 * 
	 * @return　移動されたアイテムのリスト
	 */
	public List<Item> getSortedItems() {
		return new ArrayList<Item>(mSortedItems.values());
	}

	@Override
	protected void onDrop(int fromPosition, int toPosition) {
		swapItem(fromPosition, toPosition);
	}

	@Override
	public Item getItem(int position) {
		Item item;
		if (mSortedItems.containsKey(position)) {
			item = mSortedItems.get(position);
		} else {
			Cursor c = (Cursor) super.getItem(position);
			long id = c.getLong(indexId);
			String name = c.getString(indexName);
			int playOrder = c.getInt(indexPlayOrder);

			item = new Item();
			item.mId = id;
			item.mName = name;
			item.mOldPlayOrder = playOrder;
			item.mNewPlayOrder = playOrder;
		}

		return item;
	}

	@Override
	public void bindView(View view, Context context, Cursor c) {
		final Item item = getItem(c.getPosition());
		ViewHolder holder = (ViewHolder) view.getTag();
		final String title = item.mName;
		holder.textName.setText(title);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = super.newView(context, cursor, parent);
		ViewHolder holder = new ViewHolder();
		holder.textName = (TextView) view.findViewById(android.R.id.text1);
		view.setTag(holder);
		return view;
	}

	private static class ViewHolder {
		TextView textName;
	}

	public class Item {
		private long mId;
		private String mName;
		private int mNewPlayOrder;
		private int mOldPlayOrder;

		private boolean isChanged() {
			return mOldPlayOrder != mNewPlayOrder;
		}

		public long getId() {
			return mId;
		}

		public int getPlayOrder() {
			return mNewPlayOrder;
		}

		@Override
		public String toString() {
			return String.format("id:%s, oldOrder:%s, newOrder:%s", mId,
					mOldPlayOrder, mNewPlayOrder);
		}
	}
}
